package com.spic.business.train.trainCertificate.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.spic.business.common.core.utils.poi.BusinessExcelUtil;
import com.spic.business.plan.comunication.domain.PlanCommunicationSupport;
import com.spic.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.business.train.trainCertificate.domain.TrainCertificate;
import com.spic.business.train.trainCertificate.service.ITrainCertificateService;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 人员证书Controller
 * 
 * @author spic
 * @date 2021-11-25
 */
@RestController
@RequestMapping("/train/trainCertificate")
public class TrainCertificateController extends BaseController
{
    @Autowired
    private ITrainCertificateService trainCertificateService;

    @GetMapping("/syncTrainCertificate")
    public void syncTrainCertificate() {
        trainCertificateService.syncTrainCertificate();
    }

    @PutMapping("/{ids}")
    public AjaxResult editBatch(@PathVariable String[] ids) {
        int rows = trainCertificateService.editBatch(ids);
        return rows > 0 ? AjaxResult.success("申请同步成功，预计2分钟同步完毕！") : AjaxResult.error();
    }

    /**
     * 查询人员证书列表
     */
    @PreAuthorize(hasPermi = "business/train:trainCertificate:list")
    @GetMapping("/list")
    public TableDataInfo list(TrainCertificate trainCertificate)
    {
        startPage();
        List<TrainCertificate> list = trainCertificateService.selectTrainCertificateList(trainCertificate);
        return getDataTable(list);
    }

    /**
     * 导出人员证书列表
     */
    @PreAuthorize(hasPermi = "business/train:trainCertificate:export")
    @Log(title = "人员证书", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainCertificate trainCertificate) throws IOException
    {
        List<TrainCertificate> list = trainCertificateService.selectTrainCertificateList(trainCertificate);
        ExcelUtil<TrainCertificate> util = new ExcelUtil<TrainCertificate>(TrainCertificate.class);
        util.exportExcel(response, list, "人员证书数据");
    }

    /**
     * 获取人员证书详细信息
     */
    @PreAuthorize(hasPermi = "business/train:trainCertificate:query")
    @GetMapping(value = "/{trainCertificateId}")
    public AjaxResult getInfo(@PathVariable("trainCertificateId") String trainCertificateId)
    {
        return AjaxResult.success(trainCertificateService.selectTrainCertificateByTrainCertificateId(trainCertificateId));
    }

    /**
     * 新增人员证书
     */
    @PreAuthorize(hasPermi = "business/train:trainCertificate:add")
    @Log(title = "人员证书", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainCertificate trainCertificate)
    {
        return toAjax(trainCertificateService.insertTrainCertificate(trainCertificate));
    }

    /**
     * 新增人员证书文件信息
     */
    @PreAuthorize(hasPermi = "business/train:trainCertificate:add")
    @Log(title = "人员证书", businessType = BusinessType.INSERT)
    @PostMapping("/addTrainCertificateFile")
    public AjaxResult addFile(@RequestBody List<TrainCertificate> trainCertificateList)
    {
        return toAjax(trainCertificateService.addTrainCertificateFile(trainCertificateList));
    }

    /**
     * 修改人员证书
     */
    @PreAuthorize(hasPermi = "business/train:trainCertificate:edit")
    @Log(title = "人员证书", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainCertificate trainCertificate)
    {
        return toAjax(trainCertificateService.updateTrainCertificate(trainCertificate));
    }

    /**
     * 删除人员证书
     */
    @PreAuthorize(hasPermi = "business/train:trainCertificate:remove")
    @Log(title = "人员证书", businessType = BusinessType.DELETE)
	@DeleteMapping("/{trainCertificateIds}")
    public AjaxResult remove(@PathVariable String[] trainCertificateIds)
    {
        return toAjax(trainCertificateService.deleteTrainCertificateByTrainCertificateIds(trainCertificateIds));
    }

    /**
     * 批量导入人员证书
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
//    @PreAuthorize(hasPermi = "business/plan:comunication:import")
    @Log(title = "人员证书", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TrainCertificate> util = new BusinessExcelUtil<TrainCertificate>(TrainCertificate.class);
        List<TrainCertificate> TrainCertificatetList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = trainCertificateService.importData(TrainCertificatetList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    /**
     * 下载导入模板
     * @return
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<TrainCertificate> util = new ExcelUtil<TrainCertificate>(TrainCertificate.class);
        util.importTemplateExcel(response, "人员证书");
    }
}
